import fuction.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.IntFunction;

/**
 * Date: 2023-05-02
 * Time: 15:29
 */
public class practice {
    public static void main(String[] args) {
        // 集合中存储一些字符串的数据 比如 张三,23 收集到student类型的数组当中

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,"zhangsan,23","lisi,24","wangwu,25");


        // 匿名内部类
        System.out.println("---------------------匿名内部类----------------------------");
        Student[] students = list.stream().map(new Function<String, Student>() {
            @Override
            public Student apply(String s) {
                String[] str = s.split(",");
                String name = str[0];
                int age = Integer.parseInt(str[1]);

                return new Student(name, age);
            }
        }).toArray(new IntFunction<Student[]>() {
            @Override
            public Student[] apply(int i) {
                return new Student[i];
            }
        });
        System.out.println(Arrays.toString(students));

        // 方法引用
        System.out.println("-------------------------方法引用--------------------------------");
        Student[] students1 = list.stream().map(Student::new).toArray(Student[]::new);

        System.out.println(Arrays.toString(students1));


        /*
         *   需求：
         *       创建集合添加学生对象
         *       学生对象属性：name，age
         *   要求：
         *       把姓名和年龄拼接成：张三-23的字符串，并放到数组当中
         *       使用方法引用完成
         * */


        ArrayList<Student> list1 = new ArrayList<>();

        list1.add(new Student("zhangsan",23));
        list1.add(new Student("lisi",24));
        list1.add(new Student("wangwu",25));

        // 匿名内部类
        System.out.println("---------------------匿名内部类----------------------------");

        String[] strings = list1.stream().map(new Function<Student, String>() {
            @Override
            public String apply(Student student) {
                return student.getName() + "-" + student.getAge();
            }
        }).toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int i) {
                return new String[i];
            }
        });

        System.out.println(Arrays.toString(strings));


        // 方法引用
        System.out.println("-------------------------方法引用--------------------------------");

        String[] strings1 = list1.stream().map(new practice()::apply).toArray(String[]::new);
        System.out.println(Arrays.toString(strings1));


    }
    public String apply(Student student) {
        return student.getName() + "-" + student.getAge();
    }
}
